package com.yami.shop.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopSetting;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.ShopSettingMapper;
import com.yami.shop.service.ShopSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 店铺设置Service实现类
 */
@Service
@Slf4j
public class ShopSettingServiceImpl extends ServiceImpl<ShopSettingMapper, ShopSetting> implements ShopSettingService {

    @Resource
    private MessageSource messageSource;

    @Override
    public ShopSetting getShopSetting(Long shopId) {
        if (shopId == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.setting.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        return getById(shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShopSetting(ShopSetting shopSetting) {
        if (shopSetting.getShopId() == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.setting.id.not.null", null, LocaleContextHolder.getLocale()));
        }
        
        ShopSetting existSetting = getById(shopSetting.getShopId());
        if (existSetting == null) {
            // 新增时生成店铺对接码
            shopSetting.setShopCode(IdUtil.fastSimpleUUID());
            
            // 设置默认值
            if (shopSetting.getAgentStatus() == null) {
                shopSetting.setAgentStatus(0); // 默认关闭代理功能
            }
            if (shopSetting.getAuditStatus() == null) {
                shopSetting.setAuditStatus(0); // 默认未审核
            }
            if (shopSetting.getShopStatus() == null) {
                shopSetting.setShopStatus(1); // 默认开启店铺
            }
            if (shopSetting.getAnnouncementPopup() == null) {
                shopSetting.setAnnouncementPopup(0); // 默认不弹窗
            }
            if (shopSetting.getStockDisplayMode() == null) {
                shopSetting.setStockDisplayMode(0); // 默认实际库存
            }
            if (shopSetting.getSalesDisplayMode() == null) {
                shopSetting.setSalesDisplayMode(0); // 默认实际销量
            }
            if (shopSetting.getFeeBearer() == null) {
                shopSetting.setFeeBearer(0); // 默认跟随系统
            }
            if (shopSetting.getOrderLockCard() == null) {
                shopSetting.setOrderLockCard(0); // 默认关闭
            }
            if (shopSetting.getLockCardTimeout() == null) {
                shopSetting.setLockCardTimeout(30); // 默认30分钟
            }
            if (shopSetting.getSecureLoginEnabled() == null) {
                shopSetting.setSecureLoginEnabled(0); // 默认关闭
            }
            if (shopSetting.getSecurityVerificationMethod() == null) {
                shopSetting.setSecurityVerificationMethod(0); // 默认邮箱验证
            }
            
            // 设置创建时间
            shopSetting.setCreateTime(LocalDateTime.now());
        }
        
        // 设置更新时间
        shopSetting.setUpdateTime(LocalDateTime.now());
        
        boolean result = saveOrUpdate(shopSetting);
        if (!result) {
            throw new YamiShopBindException(messageSource.getMessage("shop.setting.save.failed", null, LocaleContextHolder.getLocale()));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShopStatus(Long shopId, Integer shopStatus) {
        if (shopId == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.setting.id.not.null", null, LocaleContextHolder.getLocale()));
        }

        if (shopStatus == null || (shopStatus != 0 && shopStatus != 1)) {
            throw new YamiShopBindException(messageSource.getMessage("shop.status.invalid", null, LocaleContextHolder.getLocale()));
        }

        ShopSetting shopSetting = getById(shopId);
        if (shopSetting == null) {
            throw new YamiShopBindException(messageSource.getMessage("shop.setting.not.exist", null, LocaleContextHolder.getLocale()));
        }

        shopSetting.setShopStatus(shopStatus);
        shopSetting.setUpdateTime(LocalDateTime.now());

        boolean result = updateById(shopSetting);
        if (!result) {
            throw new YamiShopBindException(messageSource.getMessage("shop.status.update.failed", null, LocaleContextHolder.getLocale()));
        }
    }

}
